package com.zd.hamster.sys.util;

import org.springframework.boot.system.ApplicationHome;

/**
 * 文件工具
 * @author zhang
 *
 */
public class FileUtils {

	private static String PATH;
	
	public static String getPath2() {
		return System.getProperty("user.dir");
	}
	
	public static String getPath() {
		ApplicationHome home = new ApplicationHome(FileUtils.class);
		String path = (home.getSource() != null) ? home.getSource().getAbsolutePath() : "";
		return path;
	}
	
	/**
	 * 获取当前项目路径
	 * @return
	 */
	public static String getPath1() {
		if(PATH == null) {
			String path = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
			if(System.getProperty("os.name").contains("dows")) {
				path = path.substring(path.indexOf("/") + 1, path.length());
			}
			if(path.contains("jar")) {
				path = path.startsWith("file:") ? path.substring("file:".length()) : path;
				path = path.substring(0, path.lastIndexOf("."));
				return path.substring(0, path.lastIndexOf("/"));
			} 
			path = path.replace("target/classes/", "");
			PATH = path.substring(0, path.lastIndexOf("/"));
		}
		return PATH;
	}
}
